﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.IDAL;
using OgilvyOne.UKNow.DALFactory;

using ECCIT;
using ECCIT.Security;

namespace OgilvyOne.UKNow.BLL
{
    public class MemberBLL
    {
        public int LanguageID { get; set; }
        public MemberBLL(int langID)
        {
            this.LanguageID = langID;
        }

        //保存会员信息
        public ResultInfo Save(MemberModel model)
        {
            IDALMember use = MemberDALFactory.Create(LanguageID);
            return use.Save(model);
        }

        public IList<MemberModel> Check(string username)
        {
            IDALMember use = MemberDALFactory.Create(LanguageID);
            return use.Check(username);
        }
        public bool UserNameIsExists(string username)
        {
            IDALMember use = MemberDALFactory.Create(LanguageID);
            return use.Check(username).Count==0?false:true;
        }
        public IList<MemberModel> CheckEmail(string email)
        {
            IDALMember use = MemberDALFactory.Create(LanguageID);
            return use.CheckEmail(email);
        }
        public bool EmailIsExists(string email)
        {
            IDALMember use = MemberDALFactory.Create(LanguageID);
            return use.CheckEmail(email).Count>0?true:false;
        }
        public ResultInfo SignIn(string memberEmail, string password)
        {
            ResultInfo result = new ResultInfo(false);

            string epwd = MD5Crypto.GetMD5Hash(password);   //对密码进行MD5加密
            result = MemberDALFactory.Create(LanguageID).SignIn(memberEmail, epwd);
            return result;
        }

        /// <summary>
        /// 增加手机用户的token
        /// </summary>
        /// <param name="memberEmail"></param>
        /// <param name="password"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public ResultInfo SignIn(string memberEmail, string password,string token)
        {
            ResultInfo result = new ResultInfo(false);

            string epwd = MD5Crypto.GetMD5Hash(password);   //对密码进行MD5加密
            result = MemberDALFactory.Create(LanguageID).SignIn(memberEmail, epwd);
            if (result.Result)
            {
                if(!string.IsNullOrEmpty(token))
                    MemberDALFactory.Create(LanguageID).UpdateToekn(memberEmail, token);
            }
            return result;
        }

        //根据会员id 获取该对象
        public MemberModel getMemberByID(int? mid)
        {
            IDALMember use = MemberDALFactory.Create(LanguageID);
            return use.GetMemberModelByID(mid);
        }

        //获取所有会员
        public IList<MemberModel> selectmember()
        {
            IDALMember use = MemberDALFactory.Create(LanguageID);
            return use.GetAllMemberModels();
        }

        //根据时间条件获取会员
        public IList<MemberModel> GetMemberByTime(string staTme, string endTime)
        {
            IDALMember use = MemberDALFactory.Create(LanguageID);
            return use.QueryTime(staTme, endTime);
        }

        public IList<MemberModel> CheckGUid(string gid)
        {
            IDALMember use = MemberDALFactory.Create(LanguageID);
            return use.CheckGuid(gid);
        }

        /// <summary>
        /// 取MyProfile 
        /// </summary>
        /// <param name="memberid"></param>
        /// <param name="begintime"></param>
        /// <param name="endtime"></param>
        /// <returns></returns>
        public DataTable GetMyProfile(int memberid,string begintime,string endtime)
        {
            return MemberDALFactory.Create(LanguageID).GetMyProfile(memberid, begintime, endtime);
        }

        /// <summary>
        /// MyProfile Interests 所有兴趣
        /// </summary>
        /// <param name="memberid"></param>
        /// <returns></returns>
         public DataTable GetMyInterestList(int memberid)
         {
             return MemberDALFactory.Create(LanguageID).GetMyInterests(memberid);
         }
         
         /// <summary>
         /// 取个人的所有兴趣
         /// </summary>
         /// <param name="memberid"></param>
         /// <returns></returns>
         public DataTable GetMyInterest(int memberid)
         {
             DataTable dt = GetMyInterestList(memberid);
             if (dt != null && dt.Rows.Count > 0)
             {
                 DataTable returndt = dt.Clone();
                 DataRow[] dr = dt.Select("COL_MEMBER_ID=" + memberid);
                 foreach (DataRow t in dr)
                 {
                     returndt.ImportRow((DataRow) t);
                 }
                 return returndt;
             }
             return null;

         }

        /// <summary>
         /// 取个人的兴趣 设计,文学,音乐,表演艺术
        /// </summary>
        /// <param name="memberid"></param>
        /// <returns></returns>
        public string GetMyInterestStr(int memberid)
        {
            var dt = GetMyInterest(memberid);
            if (dt != null && dt.Rows.Count > 0)
            {
                string interests = dt.Rows.Cast<DataRow>().Aggregate(string.Empty,
                                                                                 (current, row) =>
                                                                                 current + (row["COL_FORM_NAME"] + ","));
                if (interests.Length > 0)
                    interests = interests.Substring(0, interests.Length - 1);
                return interests;
            }
            return string.Empty;
        }

        /// <summary>
        /// 取我的账记左边的信息
        /// </summary>
        /// <param name="memberid"></param>
        /// <returns></returns>
        public  DataTable GetMyAccount(int memberid)
        {
            return MemberDALFactory.Create(LanguageID).GetMyAccount(memberid);
        }

        /// <summary>
        /// 验证用户修改密码是否一致
        /// </summary>
        /// <param name="pw"></param>
        /// <param name="memberid"></param>
        /// <returns></returns>
        public bool CheckPwEqual(string pw,int memberid)
        {
            var model = getMemberByID(memberid);
            return model.Password.Equals(MD5Crypto.GetMD5Hash(pw));

        }
        /// <summary>
        /// 验证用户修改息信和补充信息
        /// </summary>
        /// <param name="email"></param>
        /// <param name="memberid"></param>
        /// <returns></returns>
        public bool CheckEmail(string email, int memberid)
        {
            var count = CheckEmail(email).Count;
            var model = getMemberByID(memberid);
             if(email.Equals(model.Email))
             {
                 return count > 1;
             }
             else
             {
                 return count > 0;
             }
      
        }

       /// <summary>
       /// 注销会员
       /// </summary>
       /// <param name="cnContent">中文评论内容</param>
       /// <param name="enContent">英文评论内容</param>
       /// <param name="memberid">会员ID</param>
       /// <returns></returns>
       public ResultInfo CancelMember(string cnContent, string enContent, int memberid)
       {
           return MemberDALFactory.Create(LanguageID).CancelMember(cnContent, enContent, memberid);
       }
      
      /// <summary>
      /// 验证用户名是否存在 修改的时候用的 true 为存在 false 不存在
      /// </summary>
      /// <param name="username"></param>
      /// <param name="inputname"></param>
      /// <returns></returns>
      public bool CheckUser(string username, string inputname)
      {
          return MemberDALFactory.Create(LanguageID).CheckUser(username, inputname);
      }
     
      /// <summary>
      /// 根据用户ID和语言ID获取用户的上传数，赞数，评论数，收藏活动数
      /// </summary>
      /// <param name="memberid"></param>
      /// <param name="lanagerid"></param>
      /// <returns></returns>
      public  MemberExt GetUserEventCount(int memberid,int lanagerid)
      {
          return MemberDALFactory.Create(LanguageID).GetUserEventCount(memberid, lanagerid);
      }

         /// <summary>
         /// 更新会员头像
         /// </summary>
         /// <param name="model"></param>
         /// <returns></returns>
     public bool UpdateMemberImg(MemberModel model,bool flag)
     {
         return MemberDALFactory.Create(LanguageID).UpdateMemberImg(model, flag);
     }



    }
}
